% fname: buffer_4__routing_dyad_0_6__sel_bufferlevel__topology_8x8__.m
% ./noxim -buffer 4 -routing dyad 0.6 -sel bufferlevel -dimx 8 -dimy 8  -sim 10000 -warmup 2000 -size 8 8 -hs 27 0.2 -hs 28 0.2 -hs 35 0.2 -hs 36 0.2 

function [max_pir, max_throughput, min_delay] = buffer_4__routing_dyad_0_6__sel_bufferlevel__topology_8x8__(symbol)

data = [
%             pir      avg_delay     throughput      max_delay       rpackets         rflits
           0.0018        16.1508      0.0152521            105            915           7321
           0.0018        17.2729       0.015916            119            971           7767
           0.0018         15.966      0.0149611            108            912           7301
           0.0018        16.4984      0.0146845             82            927           7401
           0.0018        16.4891      0.0152583             90            914           7324
           0.0018        16.1164      0.0157119             82            928           7416
           0.0018        17.3291      0.0153851             94            954           7631
           0.0018        16.5338      0.0150484            111            933           7464
           0.0018        18.8485      0.0148115            153            904           7228
           0.0018        17.6573      0.0140794            124            887           7096
           0.0018        16.2242      0.0146189             78            892           7134
           0.0018        16.6557      0.0153835             91            909           7261
           0.0018         16.272      0.0153229            100            919           7355
           0.0018        15.9473      0.0146734             88            910           7278
           0.0018        16.4214      0.0140444             96            871           6966
           0.0018        17.9184       0.014875            149            907           7259
           0.0018        16.7659      0.0143854            103            863           6905
           0.0018        16.4415      0.0149396            146            897           7171
           0.0018        17.1444        0.01575            103            914           7308
           0.0018        18.2002      0.0153238            129            934           7478
            0.002        18.1255      0.0164795            106           1004           8042
            0.002        18.3571      0.0175968            117           1092           8728
            0.002        18.5495      0.0162695            125           1041           8330
            0.002         17.297      0.0162857            169           1027           8208
            0.002        17.6883      0.0169365            145           1033           8265
            0.002        19.0198      0.0160198            114           1009           8074
            0.002        17.6292      0.0156411            131            971           7758
            0.002        17.8935      0.0166209            103           1014           8111
            0.002        19.1393      0.0173094            136           1055           8447
            0.002        19.7877      0.0173631            145           1093           8751
            0.002         16.998      0.0159297             92           1018           8156
            0.002        17.5753      0.0156855            108           1003           8031
            0.002        19.9732      0.0177131            162           1081           8644
            0.002        18.0829       0.016244            110           1025           8187
            0.002        18.3313       0.015994            134           1008           8061
            0.002        17.9529      0.0164375             99           1019           8153
            0.002        18.0833      0.0160464            144            996           7959
            0.002        17.8063      0.0163008            122           1043           8346
            0.002        18.0289      0.0164762            123           1038           8304
            0.002        20.3711      0.0173992            179           1078           8630
           0.0022        19.7824      0.0182242            182           1149           9185
           0.0022        20.5527      0.0180143            109           1100           8791
           0.0022        17.9794       0.017457            178           1116           8938
           0.0022        19.1281      0.0169277            130           1085           8667
           0.0022        20.8418      0.0183566            145           1119           8958
           0.0022        18.6436      0.0171172            115           1097           8764
           0.0022        19.2613       0.017498            118           1102           8819
           0.0022        22.8189      0.0184742            184           1165           9311
           0.0022        17.8173      0.0170391             89           1089           8724
           0.0022        20.1098      0.0190963            118           1166           9319
           0.0022        22.0758      0.0186508            190           1174           9400
           0.0022          17.71      0.0181332             85           1107           8849
           0.0022         19.391      0.0182619            111           1151           9204
           0.0022        19.5688      0.0180527            181           1155           9243
           0.0022        18.7825       0.018166            114           1108           8865
           0.0022        21.1068       0.018121            132           1142           9133
           0.0022        20.3986      0.0188934            145           1154           9220
           0.0022        19.1686      0.0175215            125           1121           8971
           0.0022        18.7067      0.0184254            138           1142           9139
           0.0022        18.4486      0.0171563            156           1099           8784
           0.0024        20.6321      0.0200665            155           1245           9953
           0.0024        21.2772      0.0195202            156           1212           9682
           0.0024        21.7602      0.0195179            198           1230           9837
           0.0024         22.052      0.0195553            200           1192           9543
           0.0024        20.9013      0.0197401            151           1246           9949
           0.0024        23.5297      0.0194238            211           1244           9945
           0.0024        21.7855       0.019584            123           1254          10027
           0.0024        20.6588      0.0194087            131           1222           9782
           0.0024        23.3854       0.020619            190           1300          10392
           0.0024        24.1327      0.0204234            233           1266          10130
           0.0024         20.131      0.0181133            135           1160           9274
           0.0024        21.4517      0.0190996            165           1222           9779
           0.0024        24.2472      0.0204899            224           1270          10163
           0.0024        22.4902      0.0196835            159           1222           9763
           0.0024        20.2122      0.0195159            125           1230           9836
           0.0024        19.4245      0.0187762            118           1166           9313
           0.0024        21.4145      0.0194234            130           1204           9634
           0.0024        22.7396      0.0205972            163           1298          10381
           0.0024        19.3236      0.0191582            138           1227           9809
           0.0024        21.6616      0.0193125            180           1238           9888
           0.0026        32.2718      0.0223909            808           1409          11285
           0.0026          25.29      0.0208008            197           1331          10650
           0.0026        23.8941      0.0208223            148           1332          10661
           0.0026        22.4036      0.0207734            162           1328          10636
           0.0026        23.6536      0.0209623            146           1322          10565
           0.0026        22.8976      0.0210726            228           1309          10452
           0.0026        22.1954      0.0203125            155           1300          10400
           0.0026         26.057      0.0211055            310           1352          10806
           0.0026        24.9084      0.0207581            202           1288          10296
           0.0026        25.0669      0.0210449            261           1346          10775
           0.0026        21.3842      0.0202679            147           1278          10215
           0.0026        27.0059      0.0216587            318           1365          10916
           0.0026        27.9486      0.0219028            346           1380          11039
           0.0026        22.2908      0.0217278            174           1348          10777
           0.0026        25.2125      0.0207422            233           1327          10620
           0.0026        22.3037      0.0215933            105           1360          10883
           0.0026        21.5175      0.0207339            149           1285          10284
           0.0026        23.6968      0.0206815            179           1283          10258
           0.0026        24.5067      0.0210059            209           1344          10755
           0.0026        26.1055      0.0222984            141           1384          11060
           0.0028        24.6452      0.0219306            126           1381          11053
           0.0028        28.9161      0.0232679            244           1466          11727
           0.0028        27.4437      0.0228945            154           1465          11722
           0.0028        24.1541      0.0215137            148           1376          11015
           0.0028        25.8665      0.0225972            296           1423          11389
           0.0028        27.6842      0.0224476            295           1390          11134
           0.0028        25.2282      0.0220575            302           1389          11117
           0.0028        24.1842      0.0218828            204           1401          11204
           0.0028        27.7537      0.0224603            263           1413          11320
           0.0028        30.4777      0.0234841            238           1478          11836
           0.0028        39.6367      0.0236726            705           1492          11931
           0.0028        29.4153      0.0226074            359           1447          11575
           0.0028        26.9796       0.022918            255           1467          11734
           0.0028        27.1274      0.0225556            257           1421          11368
           0.0028         29.464      0.0226825            397           1429          11432
           0.0028         24.689      0.0230726            174           1431          11444
           0.0028        26.0405      0.0227363            190           1456          11641
           0.0028        29.1699      0.0216914            298           1389          11106
           0.0028        27.7406       0.021625            363           1384          11072
           0.0028        24.5673       0.022621            202           1426          11401
            0.003        36.5515        0.02575            300           1572          12566
            0.003         31.141      0.0233594            308           1496          11960
            0.003        32.1773      0.0245313            260           1568          12560
            0.003        35.3184      0.0243633            434           1558          12474
            0.003        31.7801      0.0243711            301           1560          12478
            0.003        40.0969      0.0262734            323           1682          13452
            0.003        35.4918      0.0242837            398           1529          12239
            0.003        38.0659      0.0247218            386           1532          12262
            0.003        37.5881      0.0245159            565           1544          12356
            0.003        47.1566      0.0248438            511           1590          12720
            0.003         33.495      0.0236113            300           1511          12089
            0.003        33.1655      0.0247383            270           1583          12666
            0.003        29.9794      0.0246905            377           1556          12444
            0.003        33.3407      0.0238828            267           1529          12228
            0.003        26.6038      0.0235645            172           1507          12065
            0.003        34.6081      0.0242302            505           1526          12212
            0.003        32.1769      0.0249863            244           1600          12793
            0.003        38.5925      0.0250363            620           1551          12418
            0.003        26.7208      0.0235278            174           1483          11858
            0.003        34.1245      0.0262705            258           1606          12820
           0.0032        35.1858      0.0258145            370           1652          13217
           0.0032        29.3935      0.0244043            258           1563          12495
           0.0032        53.8376      0.0261465            612           1675          13387
           0.0032        37.5321      0.0255449            340           1635          13079
           0.0032        44.7133      0.0267837            473           1688          13499
           0.0032        38.3204      0.0260957            300           1670          13361
           0.0032        33.5853      0.0250898            367           1606          12846
           0.0032        40.9765      0.0252969            524           1619          12952
           0.0032        33.1533      0.0256973            235           1644          13157
           0.0032        39.1131      0.0255371            347           1635          13075
           0.0032        51.7314      0.0258887            525           1657          13255
           0.0032        43.5558         0.0255            474           1630          13056
           0.0032        33.5712      0.0242207            243           1551          12401
           0.0032        71.3686      0.0265859           1655           1701          13612
           0.0032        34.9223      0.0251348            266           1609          12869
           0.0032        37.8108      0.0248496            435           1591          12723
           0.0032        46.3494      0.0268633            606           1720          13754
           0.0032        41.8954      0.0259492            446           1663          13286
           0.0032        55.8047       0.026873            785           1720          13759
           0.0032        41.1052      0.0255312            368           1635          13072
           0.0034        43.6158      0.0264434            596           1692          13539
           0.0034         69.687      0.0279082            693           1786          14289
           0.0034        77.0704      0.0279746           1095           1790          14323
           0.0034         61.776      0.0267441           1035           1710          13693
           0.0034        281.248      0.0288867           3095           1849          14790
           0.0034        57.4317      0.0270078            678           1728          13828
           0.0034        36.9887      0.0261934            558           1676          13411
           0.0034        65.8547      0.0274167            713           1727          13818
           0.0034        47.8791      0.0268887            409           1721          13767
           0.0034        43.9506      0.0270099            500           1701          13613
           0.0034        74.0222      0.0281602           1356           1803          14418
           0.0034         95.763      0.0273145           1651           1747          13985
           0.0034        44.4099      0.0272227            467           1742          13938
           0.0034        62.5119      0.0275527            618           1764          14107
           0.0034        49.8495      0.0273164            579           1748          13986
           0.0034        99.8137      0.0277715           1377           1777          14219
           0.0034        70.6834      0.0267871            967           1715          13715
           0.0034        59.9536      0.0269434            582           1725          13795
           0.0034        60.2846      0.0277832            819           1778          14225
           0.0034          40.52       0.025457            547           1629          13034
           0.0036        118.325      0.0289355           2230           1853          14815
           0.0036        70.9616      0.0284688           1302           1822          14576
           0.0036         144.06      0.0280215           1862           1792          14347
           0.0036        84.7873      0.0285586           1252           1829          14622
           0.0036        126.148      0.0290898           1825           1863          14894
           0.0036        65.3994      0.0283574            580           1815          14519
           0.0036        55.1429      0.0281211            696           1799          14398
           0.0036         109.95      0.0292754           1755           1872          14989
           0.0036        132.893      0.0281797           1453           1804          14428
           0.0036        316.006      0.0288828           4073           1848          14788
           0.0036        313.371      0.0293988           3573           1854          14817
           0.0036        74.8857      0.0289688           1039           1855          14832
           0.0036        80.1486          0.029           1310           1857          14848
           0.0036        274.575      0.0290996           3689           1863          14899
           0.0036        73.0847      0.0285742           1206           1829          14630
           0.0036         195.59      0.0289414           2729           1855          14818
           0.0036         66.697       0.029043            905           1858          14870
           0.0036        78.5524      0.0286035            977           1832          14645
           0.0036        80.4769      0.0290313           1136           1860          14864
           0.0036        154.201      0.0287012           3282           1837          14695
];

rows = size(data, 1);
cols = size(data, 2);

data_delay = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+1);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_delay = [data_delay; data(ifirst, 1:cols-5), avg ci];
end

figure(1);
hold on;
plot(data_delay(:,1), data_delay(:,2), symbol);

data_throughput = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+2);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_throughput = [data_throughput; data(ifirst, 1:cols-5), avg ci];
end

figure(2);
hold on;
plot(data_throughput(:,1), data_throughput(:,2), symbol);

data_maxdelay = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+3);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_maxdelay = [data_maxdelay; data(ifirst, 1:cols-5), avg ci];
end

figure(3);
hold on;
plot(data_maxdelay(:,1), data_maxdelay(:,2), symbol);


%-------- Saturation Analysis -----------
slope=[];
for i=2:size(data_throughput,1),
    slope(i-1) = (data_throughput(i,2)-data_throughput(i-1,2))/(data_throughput(i,1)-data_throughput(i-1,1));
end

for i=2:size(slope,2),
    if slope(i) < (0.95*mean(slope(1:i)))
        max_pir = data_throughput(i, 1);
        max_throughput = data_throughput(i, 2);
        min_delay = data_delay(i, 2);
        break;
    end
end
